﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.SqlClient;

namespace SimpleLibrary
{
    // This Class contains Exception Information
    public class ExceptionStatistics
    {
        private static Queue<ExceptionStatistics> myQueue = new Queue<ExceptionStatistics>();
        private string task;
        private string strQuery;
        private string strException;
        private string strInnerException;
        private string strParameters;

        // Insert Query Execution information
        public void Insert(string task,string strQuery,Exception ex,SqlCommand comm){
            lock (myQueue)
            {
                ExceptionStatistics stats = new ExceptionStatistics();
                stats.task = task;
                stats.strQuery = strQuery;
                stats.strException = "<b>" + ex.Message + "</b><br>" + ex.StackTrace;
                stats.strInnerException = "";
                if (ex.InnerException != null)
                {
                    stats.strInnerException = "<b>" + ex.InnerException.Message + "</b><br>" + ex.InnerException.StackTrace;
                }
                stats.strParameters = "";
                StringBuilder sb = new StringBuilder();
                foreach (SqlParameter localParam in comm.Parameters)
                {
                    try
                    {
                        sb.Append("<br>" + localParam.ParameterName + ":" + localParam.Value.ToString());
                    }
                    catch
                    {
                        //If there's an exception during access parameters, Do nothing;
                    }
                }
                myQueue.Enqueue(stats);
            }
        }

        public static int GetExceptionCount()
        {
            return ExceptionStatistics.myQueue.Count;
        }

        // Gather statistics information and Generate result HTML file.
        public static void GenerateReport(DateTime mydate)
        {
            var status =
                from queue in myQueue
                where queue != null
                select new { Task = queue.task, Query = queue.strQuery , Error = queue.strException, InnerException = queue.strInnerException, Parameters = queue.strParameters };

            StringBuilder sb = new StringBuilder();
            sb.Append("<html>");
            sb.Append("<head>");
            sb.Append("<style type='text/css'>");
            sb.Append(" table.example2 {background-color:transparent;border-collapse:collapse;width:100%;} ");
            sb.Append(" table.example2 th, table.example2 td {text-align:left;border:1px solid black;padding:5px;} ");
            sb.Append(" table.example2 th {background-color:AntiqueWhite;} ");
            sb.Append(" table.example2 td:first-child {width:20%;} ");
            sb.Append("</style> ");
            sb.Append("</head>");
            sb.Append("<body>");
            sb.Append("<h1>SimpleLoadGenertor Exception Report</h1>");
            sb.Append("<p>");
            sb.Append("<h2>Exception Information</h2> <br>");
            sb.Append("<table class='example2'>");
            sb.Append("<tr>");
            sb.Append("<th> Task</th>");
            sb.Append("<th> Query</th>");
            sb.Append("<th> Exception </th>");
            sb.Append("<th> InnerException </th>");
            sb.Append("<th> SqlParameters </th>");
            sb.Append("</tr>");

            foreach (var stats in status)
            {
                sb.Append("<tr>");
                sb.Append("<td>" + stats.Task + "</td>");
                sb.Append("<td>" + stats.Query + "</td>");
                sb.Append("<td>" + stats.Error + "</td>");
                sb.Append("<td>" + stats.InnerException + "</td>");
                sb.Append("<td>" + stats.Parameters + "</td>");
                sb.Append("</tr>");
            }

            sb.Append("</table>");
            sb.Append("</body>");
            sb.Append("</html>");

            using (FileStream fs = new FileStream("Error" + mydate.ToShortDateString() + ".html", FileMode.Create))
            {
                using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
                {
                    w.WriteLine(sb.ToString());
                }
            }
        }
    }

    
}
